<?php

namespace ctrl;

use model\admin_group;
use libs\view;

class admingroup extends base
{
	private static $M;
	public static function Init()
	{
		parent::init();
		self::$M = new admin_group;
	}

	public static function index()
	{
		if (isset($_GET['list_json'])) {
			jsonList(self::$M->jsonPageSelect());
		} else {
			view::display();
		}
	}

	public static function add()
	{
		if (isset($_GET['view'])) {
			view::assign('info', null);
			view::assign('title', "添加角色");
			view::display("post");
		} else {
			if (empty($_POST['groupname'])) {
				Response(array('status' => 0, 'info' => '请填写角色名！'));
			}
			$result = self::$M->insertData();
			Response(array('status' => $result['status'], 'info' => $result['msg']));
		}
	}

	public static function edit()
	{
		if (isset($_GET['view'])) {
			$info = self::$M->findData();
			view::assign('info', $info);
			view::assign('title', "编辑角色名及权限");
			view::display("post");
		} else if (isset($_GET['channlroleview'])) {
			self::ChannlRole();
		} elseif (isset($_GET['channlrolepost'])) {
			self::ChannlRolePost();
		} else {
			if (empty($_POST['groupname'])) {
				Response(array('status' => 0, 'info' => '请填写角色名！'));
			}
			$result = self::$M->updateData();
			Response(array('status' => $result['status'], 'info' => $result['msg']));
		}
	}

	public static function del()
	{
		if ($_POST['id'] == 1) {
			Response(array('status' => 0, 'info' => '不能删除超级管理员组！'));
		}
		if (self::$M->deleteOneData()) {
			Response(array('status' => 1, 'info' => '删除成功'));
		} else {
			Response(array('status' => 0, 'info' => '该用户组下有用户，请先删除该组下管理员用户！'));
		}
	}

	//内容栏目设置,edit私有调用
	private static function ChannlRole()
	{
		$info = self::$M->findData();
		$res = [
			'channlid' => empty($info['channlrole']) ? '' : implode(",", JsonDecode($info['channlrole'])),
			'groupname' => $info['groupname'],
			'id' => $info['id']
		];
		view::assign('info', $res);
		view::display('channlrole');
	}
	private static function ChannlRolePost()
	{
		$result = self::$M->updateChannlroleData();
		Response(array('status' => $result['status'], 'info' => $result['msg']));
	}
}
